package com.caht.servier.Impl;

import com.caht.DTO.CommentReplyCommentsDTO;
import com.caht.VO.CommentReplyCommentsVO;
import com.caht.VO.ReplyCommentsVO;
import com.caht.context.BaseContext;
import com.caht.entity.comment;
import com.caht.entity.replyComment;
import com.caht.mapper.commentMapper;
import com.caht.servier.commentServier;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class commentServierImpl implements commentServier {
    @Autowired
    private commentMapper commentmapper;
    /**
     * 查询所有 评论
     * @param postsId
     * @return
     */
    @Override
    public List<CommentReplyCommentsVO> queryAll(Long postsId) {
        List<CommentReplyCommentsVO> commentReplyCommentsVOS = commentmapper.CommentAll(postsId);
        for (CommentReplyCommentsVO commentReplyCommentsVO : commentReplyCommentsVOS) {
            //查询所有回复的品论
            List<ReplyCommentsVO> replyComment =commentmapper.queryAllReplyComment(commentReplyCommentsVO.getId());
            commentReplyCommentsVO.setChildComment(replyComment);
        }
        return commentReplyCommentsVOS;
    }

    /**
     * 评论
     * @param replyCommentsDTO
     */
    @Override
    public void addCommentReplyComments(CommentReplyCommentsDTO replyCommentsDTO) {

        Long currentId = BaseContext.getCurrentId();
        if(replyCommentsDTO.getPostsId() == 0){
            replyComment replycomment = new replyComment();
            BeanUtils.copyProperties(replyCommentsDTO, replycomment);
            replycomment.setCommentId((long) replyCommentsDTO.getId());
            replycomment.setReplyUserId(currentId);
            replycomment.setCreationTime(LocalDateTime.now());
            replycomment.setUpdateTime(LocalDateTime.now());
            commentmapper.addreplyComment(replycomment);
        } else {
            comment comment  = new comment();
            BeanUtils.copyProperties(replyCommentsDTO, comment);
            comment.setCommentUserId(currentId);
            comment.setCreationTime(LocalDateTime.now());
            comment.setUpdateTime(LocalDateTime.now());
            commentmapper.addComment(comment);
        }
    }
}
